package hrms.service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import hrms.dao.AdminDao;
import hrms.dao.EmpDao;
import hrms.dao.HistoryDao;
import hrms.entity.Admin;
import hrms.entity.Emp;
import hrms.entity.History;
import hrms.util.PageUtil;
import hrms.util.PwdUtil;
import hrms.util.Result;

@Service("adminService")
public class AdminServiceImpl implements AdminService{

	@Resource(name="adminDao")
	private AdminDao dao ;
	
	@Resource(name="historyDao")
	private HistoryDao hisDao;
	
	@Resource(name="empDao")
	private EmpDao empDao;
	
	public Result<Admin> check(String adminCode, String pwd) {
		Result<Admin> result = new Result<Admin>();
		Admin user = dao.findByAdminCode(adminCode);
		if (user != null) {
			String password = user.getPassword();
			String md5Pwd = PwdUtil.md5(pwd);
			if (password.equals(md5Pwd)) {
				result.setStatus(0);
				result.setMsg("登录成功!");
				Timestamp t = new Timestamp(System.currentTimeMillis());
				History history = new History();
				history.setAdminCode(user.getAdminCode());
				history.setEmpId(user.getEmpId());
				history.setEmpName(user.getEmpName());
				history.setEnrolldate(t);
				history.setOutdate(t);
				history.setRoll(user.getRoll());
				hisDao.saveHistory(history);
				Map<Object,Object> map = new HashMap<Object,Object>();
				map.put("enrolldate",t);
				map.put("adminId", user.getAdminId());
				dao.updateEnrolldate(map);
				user.setEnrolldate(t);
				result.setData(user);
			} else {
				result.setStatus(2);
				result.setMsg("密码不正确 !");
			}
		} else {
			result.setStatus(1);
			result.setMsg("帐号不存在 !");
		}
		return result;
	
	}
	
	public void outLog(HttpServletRequest req) {
		Admin user = (Admin) req.getSession().getAttribute("admin");
		if(user != null){
			hisDao.updateHistory(new Timestamp(System.currentTimeMillis()),user.getEnrolldate());
		}
	}

	public Result<Object> changePwd(String oldPwd, String newPwd, Integer adminId) {
		Result<Object> result = new Result<Object>();
		Admin admin = dao.findByAdminId(adminId);
		String md5OldPwd = admin.getPassword();
		String md5OldPwdInput = PwdUtil.md5(oldPwd);
		if(md5OldPwd.equals(md5OldPwdInput)){
			Map<Object,Object> map = new HashMap<Object,Object>();
			String md5NewPwd = PwdUtil.md5(newPwd);
			map.put("adminId", adminId);
			map.put("password", md5NewPwd);
			int num = dao.updatePwd(map);
			if( num != 1){
				result.setStatus(1);
				result.setMsg("修改密码出错!");
				return result;
			} else {
				result.setStatus(0);
				result.setMsg("修改密码成功!");
				return result;
			}
			
		} else {
			result.setStatus(2);
			result.setMsg("旧密码错误!");
			return result;
		}
	}

	public Result<Object> addAdmin(Integer empId, String adminCode, String empName, String pwd, Integer roll) {
		Result<Object> result = new Result<Object>();
		Emp emp = empDao.findByEmpId2(empId); //此查找方法需要判断为人事部的员工  
		if(emp != null){
			if(emp.getEmpName().equals(empName)){
				Admin ad = dao.findByEmpId(empId);
				if(ad != null){
					result.setStatus(7);
					result.setMsg("该员工已拥有帐号!");
					return result;
				}
				Admin a = dao.findByAdminCode(adminCode);
				if(a != null){
					result.setStatus(2);
					result.setMsg("用户名已存在!");
					return result;
				} else {
					Timestamp t = new Timestamp(System.currentTimeMillis());
					Admin admin = new Admin();
					admin.setAdminCode(adminCode);
					admin.setEmpId(empId);
					admin.setEmpName(empName);
					admin.setRoll(roll);
					admin.setPassword(PwdUtil.md5(pwd));
					admin.setEnrolldate(t);
					admin.setCreateTime(t);
					int num = dao.addAdmin(admin);
					if(num != 1){
						result.setStatus(1);
						result.setMsg("用户创建出错!");
						return result;
					} else {
						result.setStatus(0);
						result.setMsg("用户创建成功!");
						return result;
					}
				}
			} else {
				result.setStatus(3);
				result.setMsg("员工号与名字不匹配!");
				return result;
			}
		} else {
			result.setStatus(4);
			result.setMsg("员工号不存在或不属于人事部!");
			return result;
		}
	}
	
	public Result<List<Admin>> findByPage(String pages ,HttpServletResponse res) {
		String message = "Admin";
		return new PageUtil<Admin>().findByPage(pages, res, dao, message);
	}
	
	public Result<Map<String, Integer>> pageTotal(HttpServletRequest req){
		String message = "Admin";
		return PageUtil.pageTotal(req, message);
	}
	
	public Result<Object> deleteAdmin(String adminId ,HttpServletRequest req) {
		//获取当前登录的用户
		Admin adminUser = (Admin)req.getSession().getAttribute("admin");
		Integer rollUser = adminUser.getRoll();
		//获取将要被删除的用户
		Admin admin = dao.findOne(Integer.parseInt(adminId));
		Integer roll = admin.getRoll();
		if(rollUser==1 && roll==1){
			Result<Object> result = new Result<Object>();
			result.setStatus(2);
			result.setMsg("权限不够!");
			return result;
		}
		Result<Object> result = new Result<Object>();
		int id = Integer.parseInt(adminId);
		int num = dao.deleteAdmin(id);
		Cookie[] c = req.getCookies();
		String page = "1";
		for(Cookie cc : c){
			if(cc.getName().equals("AdminPage")){
				page = cc.getValue();
			}
		}
		int pageNum = Integer.parseInt(page);
		if(num != 1){
			result.setStatus(1);
			result.setMsg("删除出错");
		} else {
			result.setStatus(0);
			result.setMsg("删除成功");
			result.setData(pageNum);
		}
		return result;
	}

	public Result<List<Admin>> search(String name) {
		// TODO Auto-generated method stub
		return null;
	}

}